unit outputinvoiceslist;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  Buttons, DBGrids, DbCtrls;

type

  { TfrmOutPutInvoicesList }

  TfrmOutPutInvoicesList = class(TForm)
    btnAdd: TBitBtn;
    btnClose: TBitBtn;
    btnDelete: TBitBtn;
    btnEdit: TBitBtn;
    DBGOutPutInvoicesList: TDBGrid;
    procedure btnAddClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure DBGOutPutInvoicesListDblClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure checkErrors;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  frmOutPutInvoicesList: TfrmOutPutInvoicesList;

implementation

{$R *.lfm}

uses main,outputinvoiceselement;

{ TfrmOutPutInvoicesList }

procedure TfrmOutPutInvoicesList.checkErrors;
begin
  if (DBGOutPutInvoicesList.DataSource.DataSet.RecordCount=0) then begin
    btnEdit.Enabled:=False;
    btnDelete.Enabled:=False;
  end else begin
    btnEdit.Enabled:=True;
    btnDelete.Enabled:=True;
  end;
end;

procedure TfrmOutPutInvoicesList.btnDeleteClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Для правильного видалення потрібно зайти в накладну і видалити товари по одному після чого видалити накладну, продовжити незважаючи на це?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    frmMain.execQuery('DELETE FROM outputgoods WHERE invoiceid='+DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.execQuery('DELETE FROM outputofinvoicesongoods WHERE id='+DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.history('Видаткова накладна','Видалення накладної № '+DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.DSOutPutInvoices.DataSet.Refresh;
    checkErrors;
  end;
end;

procedure TfrmOutPutInvoicesList.btnAddClick(Sender: TObject);
begin
  frmOutputInvoicesElement.invoiceIsNew := True;
  frmOutputInvoicesElement.ShowModal;
  checkErrors;
end;

procedure TfrmOutPutInvoicesList.btnEditClick(Sender: TObject);
begin
  frmOutputInvoicesElement.lblEdtCounterParty.Text := DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('counterpartytitle').AsString + ' ' +#13 + DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('counterpartyinfo').AsString;
  frmOutputInvoicesElement.lblEdtStoreHouse.Text := DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('storehousetitle').AsString;
  frmOutputInvoicesElement.ginvoiceid := DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('id').AsInteger;
  FrmMain.storeHouseSelectedId := DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('storehouseid').AsInteger;
  FrmMain.counterPartieSelectedId := DBGOutPutInvoicesList.DataSource.DataSet.FieldByName('counterpartyid').AsInteger;
  frmOutputInvoicesElement.invoiceIsNew := False;
  frmOutputInvoicesElement.ShowModal;
  checkErrors;
end;

procedure TfrmOutPutInvoicesList.DBGOutPutInvoicesListDblClick(Sender: TObject);
begin
  if (DBGOutPutInvoicesList.DataSource.DataSet.RecordCount>0) then begin
    btnEdit.Click;
  end;
end;

procedure TfrmOutPutInvoicesList.FormShow(Sender: TObject);
begin
  DBGOutPutInvoicesList.DataSource.DataSet.Last;
  checkErrors;
end;

end.

